package com.gqj.meter.service.impl;

import com.gqj.common.utils.DateUtils;
import com.gqj.common.utils.SecurityUtils;
import com.gqj.common.utils.StorageUtil;
import com.gqj.meter.domain.SysDataBackupRecord;
import com.gqj.meter.mapper.SysDataBackupRecordMapper;
import com.gqj.meter.service.ISysDataBackupRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 数据备份记录Service业务层处理
 *
 * @author lxq
 * @date lxq-01-09
 */
@Service
public class SysDataBackupRecordServiceImpl implements ISysDataBackupRecordService {
    @Autowired
    private SysDataBackupRecordMapper sysDataBackupRecordMapper;
    @Value("${filePath}")
    private String filePath;
    @Value("${dbName}")
    private String dbName;
    @Value("${dbUsername}")
    private String dbUsername;
    @Value("${dbPassword}")
    private String dbPassword;
    @Value("${dbIp}")
    private String dbIp;

    /**
     * 查询数据备份记录
     *
     * @param backupId 数据备份记录主键
     * @return 数据备份记录
     */
    @Override
    public SysDataBackupRecord selectSysDataBackupRecordByBackupId(Long backupId) {
        return sysDataBackupRecordMapper.selectSysDataBackupRecordByBackupId(backupId);
    }

    /**
     * 查询数据备份记录列表
     *
     * @param sysDataBackupRecord 数据备份记录
     * @return 数据备份记录
     */
    @Override
    public List<SysDataBackupRecord> selectSysDataBackupRecordList(SysDataBackupRecord sysDataBackupRecord) {
        return sysDataBackupRecordMapper.selectSysDataBackupRecordList(sysDataBackupRecord);
    }

    /**
     * 新增数据备份记录
     *
     * @param sysDataBackupRecord 数据备份记录
     * @return 结果
     */
    @Override
    public int insertSysDataBackupRecord(SysDataBackupRecord sysDataBackupRecord) {
        return sysDataBackupRecordMapper.insertSysDataBackupRecord(sysDataBackupRecord);
    }

    /**
     * 修改数据备份记录
     *
     * @param sysDataBackupRecord 数据备份记录
     * @return 结果
     */
    @Override
    public int updateSysDataBackupRecord(SysDataBackupRecord sysDataBackupRecord) {
        return sysDataBackupRecordMapper.updateSysDataBackupRecord(sysDataBackupRecord);
    }

    /**
     * 批量删除数据备份记录
     *
     * @param backupIds 需要删除的数据备份记录主键
     * @return 结果
     */
    @Override
    public int deleteSysDataBackupRecordByBackupIds(Long[] backupIds) {
        return sysDataBackupRecordMapper.deleteSysDataBackupRecordByBackupIds(backupIds);
    }

    /**
     * 删除数据备份记录信息
     *
     * @param backupId 数据备份记录主键
     * @return 结果
     */
    @Override
    public int deleteSysDataBackupRecordByBackupId(Long backupId) {
        return sysDataBackupRecordMapper.deleteSysDataBackupRecordByBackupId(backupId);
    }

    /**
     * 生成数据备份记录
     */
    public int generateRecord() {
        int i = 1;
        boolean isFlag = StorageUtil.generateRecord(filePath, dbName, dbUsername, dbPassword, dbIp);
        //记录数据备份
        if (isFlag) {
            SysDataBackupRecord sysDataBackupRecord = new SysDataBackupRecord();
            sysDataBackupRecord.setBackupTime(DateUtils.getNowDate());
            sysDataBackupRecord.setBackupType(1);
            sysDataBackupRecord.setBackupUrl(filePath);
            sysDataBackupRecord.setBackupUserId(SecurityUtils.getUserId());
            sysDataBackupRecord.setDelflag(0);
            insertSysDataBackupRecord(sysDataBackupRecord);
        }
        return i;

    }
}
